﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Configuration;

namespace Card.Utility
{
    public class ImageUtilty
    {
        /// <summary>
        /// 图片服务器地址（最后不带斜杠）
        /// </summary>
        public static string ImageServer
        {
            get
            {
                if (ConfigurationManager.AppSettings[CONST.AppSettings.ImageServer] != null)
                {
                    return ConfigurationManager.AppSettings[CONST.AppSettings.ImageServer].TrimEnd('/');
                }
                return "";
            }
        }

        public static Bitmap CreateImage(string w, Font font,Color color)
        {
            Bitmap img = new Bitmap(1, 1);
            if (!string.IsNullOrEmpty(w))
            {
                StringFormat sf = StringFormat.GenericTypographic;                

                Graphics g = Graphics.FromImage(img);
                SizeF s = g.MeasureString(w, font, new PointF(0,0),sf);
                int width = Convert.ToInt32(s.Width);
                int height = Convert.ToInt32(s.Height);
                width = width < s.Width ? width + 1 : width;
                height = height < s.Height ? height + 1 : height;

                g.Dispose();
                img.Dispose();

                img = new Bitmap(width, height);
                g = Graphics.FromImage(img);

                g.Clear(Color.FromArgb(0));
                g.DrawString(w, font, new SolidBrush(color), 0, 0,StringFormat.GenericTypographic);

                g.Dispose();
            }
            return img;
        }

        public static string GetTemplateImagePath(string imageName)
        {
            return string.Format("{0}/TemplateImages/{1}/{2}/{3}", ImageServer, imageName.Substring(0, 4), imageName.Substring(4, 2), imageName);
        }

        public static string GetTemplateThumbImagePath(string imageName)
        {
            return GetTemplateImagePath(imageName.Insert(imageName.LastIndexOf('.'), "t"));
        }

        public static string GetTemplateListImagePath(string frontImageName)
        {
            return GetTemplateImagePath(frontImageName.Replace(".","TM."));
        }

        public static string GetUserCardThumbImagePath(int cardID,int userID,string faceType, string image)
        {
            return string.Format("{0}/UserCards/{1}/{2}/{3}T.{4}", ImageServer,userID, cardID, faceType, GetFileSuffix(image));
        }
               

        public static string GetUserCardListImagePath(int cardID, int userID, string frontImage)
        {
            return string.Format("{0}/UserCards/{1}/{2}/TL.{3}", ImageServer, userID,cardID, GetFileSuffix(frontImage));
        }

        public static string GetOrderCardListImagePath(int cardID, int userID, string frontImage)
        {
            return string.Format("{0}/OrderCards/{1}/{2}/TL.{3}", ImageServer, userID, cardID, GetFileSuffix(frontImage));
        }

        public static string GetOrderCardThumbImagePath(int cardID, int userID, string faceType, string image)
        {
            return string.Format("{0}/OrderCards/{1}/{2}/{3}T.{4}", ImageServer, userID, cardID, faceType, GetFileSuffix(image));
        }

        public static string GetOrderCardImagePath(int cardID, int userID, string faceType, string image)
        {
            return string.Format("{0}/OrderCards/{1}/{2}/{3}.{4}", ImageServer, userID, cardID, faceType, GetFileSuffix(image));
        }

        public static string GetLogoPath(string logo)
        {
            return string.Format("{0}/logoImages/{1}", ImageServer, logo);
        }

        public static string GetFileSuffix(string fileName)
        {
            return fileName.Substring(fileName.LastIndexOf('.')).ToLower().TrimStart('.');
        }
        //public static string ToBase64Image(string w, Font font)
        //{
        //    Bitmap img = CreateImage(w, font);
        //    MemoryStream ms = new MemoryStream();
        //    img.Save(ms, ImageFormat.Png);
        //    byte[] arr = new byte[ms.Length];
        //    ms.Position = 0;
        //    ms.Read(arr, 0, (int)ms.Length);
        //    ms.Close();
        //    return Convert.ToBase64String(arr);
        //}
    }
}
